package com.crealth.utils;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class StringUtil {
	public static Long oneDayMillisecond = 1000*60*60*24L;
	
    public static String mergeListIntegerToString(List<Integer> ls, String space) {
        String str = "";
        if ((ls == null) || (ls.size() == 0)) {
            return str;
        }
        for (Iterator<Integer> it = ls.iterator(); it.hasNext(); ) {
            int num = ((Integer)it.next()).intValue();
            if (str.length() == 0) {
                str = num + "";
            }
            else {
                str += (space + num);
            }
        }

        return str;
    }

    public static String accumuString(String strDes, String apendStr, String space) {
        if ((strDes == null) || (strDes.length() == 0))
            strDes = apendStr;
        else {
            strDes = strDes + space + apendStr;
        }
        return strDes;
    }

    public static String mergeListToString(List<String> ls, String space) {
        String str = "";
        if ((ls == null) || (ls.size() == 0)) {
            return str;
        }

        for (String tmp : ls) {
            if (str.length() == 0) {
                str = tmp;
            }
            else {
                str += (space + tmp);
            }
        }
        return str;
    }
    
    public static String mergeStringToSql(List<String> ls) {
        String str = "";
        if ((ls == null) || (ls.size() == 0)) {
            return str;
        }

        for (String tmp : ls) {
            if (str.length() == 0) {
                str = "'" + tmp + "'";
            }
            else {
                str += (", " + "'" + tmp + "'");
            }
        }
        return str;
    }
    
    public static String mergeLongListToString(List<Long> ls, String space) {
        String str = "";
        if ((ls == null) || (ls.size() == 0)) {
            return str;
        }

        for (Long tmp : ls) {        	
            if (str.length() == 0) {
                str = "" + tmp;
            }
            else {
                str += space + tmp;
            }
        }
        return str;
    }

    public static String removeSubstring(String strSrc, String subStr) {
        if ((strSrc == null) || (strSrc.length() == 0) || 
            (strSrc.equals(subStr))) {
            return "";
        }

        String r1 = subStr + ",";
        String r2 = "," + subStr;
        String resultStr = strSrc;
        if (strSrc.indexOf(r1) == 0) {
            resultStr = strSrc.replaceFirst(r1, "");
        }
        else if (strSrc.indexOf(r2) > 0) {
            resultStr = strSrc.replaceAll(r2, "");
        }
        else if (strSrc.indexOf(subStr) == 0) {
            resultStr = strSrc.replaceFirst(subStr, "");
        }

        return resultStr;
    }
    
    public static String getTimeString() {
    	Date curDate = new Date(System.currentTimeMillis());
    	String simpleFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(curDate);
    	
    	return simpleFmt;
    }
    
    public static List<String> splitStringToList(String strSrc, String space) {
    	String[] phoneArray = strSrc.split(space);
    	List<String> resList = Arrays.asList(phoneArray);
    	
    	return resList;
    }
    
    public static List<Integer> splitStringToIntegerList(String strSrc, String space) {
    	String[] phoneArray = strSrc.split(space);
    	List<String> strList = Arrays.asList(phoneArray);
    	List<Integer> resList = new ArrayList<Integer>();
    	for (String strValue : strList) {
    		Integer curValue = Integer.valueOf(strValue);
    		if (curValue != null) {
    			resList.add(curValue);
    		}
    	}
    	
    	return resList;
    }
    
    public static String getCurrentDay() {
    	Date curDate = new Date(System.currentTimeMillis());
    	String simpleFmt = new SimpleDateFormat("yyyyMMdd").format(curDate);
    	
    	return simpleFmt;
    }
    
    public static String getPredayString(int preDay) {
    	Long curtm = System.currentTimeMillis();
    	Long tm = curtm + preDay*oneDayMillisecond;
    	Date curDate = new Date(tm);
    	String simpleFmt = new SimpleDateFormat("yyyyMMdd").format(curDate);
    	
    	return simpleFmt;
    }
    
    public static String getPreMonthString(int preMonth) {
    	Date date = new Date(System.currentTimeMillis());
    	Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, preMonth);
        Date preMonthDate = cal.getTime();
        String simpleFmt = new SimpleDateFormat("yyyyMMdd").format(preMonthDate);
    
        return simpleFmt;
    }
    
	//length表示生成字符串的长度
    public static String getRandomString(int length) {  
    	String base = "abGHcdeSTUV45WXYZfghijkls67tuKLMNOvwJPQxyzABCDmno23pqrEFIR0189";     
        Random random = new Random();     
        StringBuffer sb = new StringBuffer();     
        for (int i = 0; i < length; i++) {     
            int number = random.nextInt(base.length());     
            sb.append(base.charAt(number));     
        }
        
        return sb.toString();     
	}
    
    //  组合  Sql 条件语句
    public static String combinationSql(String strDes, String addSql) {
    	if (strDes == null || strDes.length() == 0) {
    		strDes = addSql;
    	} else {
    		strDes = strDes + " AND " + addSql;
    	}
    	
    	return strDes;
    }
    
}
